﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Client_Revenue
{
    public partial class AddContracts : System.Web.UI.Page
{

         #region "Properties & Fields"
        public string FullName = string.Empty;
        //private List<ContractLineOfBusiness> iContractLinesOfBiz = new List<ContractLineOfBusiness>();
        private ContractLineOfBusiness _contractLoB = new ContractLineOfBusiness();
        //private List<SalesInfo> iSalesInfo = new List<SalesInfo>();
        private SalesInfo _salesinfo = new SalesInfo();
        private Dictionary<int, string> theContractBizLine = new Dictionary<int, string>();

        private List<SalesInfo> iSalesInfo
        {
            get
            {
                if (this.ViewState["iSalesInfo"] == null)
                    return null;

                return (List<SalesInfo>)this.ViewState["iSalesInfo"];
            }
            set { this.ViewState["iSalesInfo"] = value; }
        }
        private List<ContractLineOfBusiness> iContractLinesOfBiz
        {
            get
            {
                if (this.ViewState["iContractLinesOfBiz"] == null)
                    return null;

                return (List<ContractLineOfBusiness>)this.ViewState["iContractLinesOfBiz"];
            }
            set { this.ViewState["iContractLinesOfBiz"] = value; }
        }
        private List<string> iLinesOfBiz
        {
            get
            {
                if (this.ViewState["iLinesOfBiz"] == null)
                    return null;

                return (List<string>)this.ViewState["iLinesOfBiz"];
            }
            set { this.ViewState["iLinesOfBiz"] = value; }
        }
#endregion


       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            { 

                LoadPageData();
            }
        }

        public void LoadPageData()
        {

            iContractLinesOfBiz = new List<ContractLineOfBusiness>();
            iSalesInfo = new List<SalesInfo>();
            iLinesOfBiz = new List<string>();

            using (var DE = new dominiumEntities())
            {
                //clients
                var theClients = (from c in DE.Clients
                                  join t in DE.ClientTypes on c.ClientTypeId equals t.ClientTypeId
                                  orderby c.ClientName ascending
                                  select new { c.ClientId, c.ClientName, t.ClientTypeName }
                                 ).Distinct().ToList();


                ddClient.DataSource = theClients;
                ddClient.DataTextField = "ClientName";
                ddClient.DataValueField = "ClientId";
                ddClient.DataBind();

                //Business Lines

                var lob = (from l in DE.LineOfBusinesses select new { l.LineOfBusinessId, l.LoBName }).Distinct().ToList();

                ddBizLine.DataSource = lob;
                ddBizLine.DataTextField = "LoBName";
                ddBizLine.DataValueField = "LineOfBusinessId";
                //chlLineOfBiz.SelectedIndex = 0;
                ddBizLine.DataBind();


                //Carriers
                var carriers = (from c in DE.Carriers select new { c.CarrierId, c.CarrierName }).Distinct().ToList();

                ddCarrier.DataSource = carriers;
                ddCarrier.DataTextField = "CarrierName";
                ddCarrier.DataValueField = "CarrierId";
                ddCarrier.DataBind();

                //Insurance Types
                var instype = (from i in DE.InsuranceTypes select new { i.InsuranceTypeId, i.InsuranceTypeName }).Distinct().ToList();

                ddInsuType.DataSource = instype;
                ddInsuType.DataTextField = "InsuranceTypeName";
                ddInsuType.DataValueField = "InsuranceTypeId";
                ddInsuType.SelectedIndex = 0;
                ddInsuType.DataBind();

                //Producers
                var theProducers = (from p in DE.People
                                    join t in DE.SalesTeams on p.PersonId equals t.PersonId
                                    orderby p.LastName
                                    select new
                                    {
                                        p.PersonId,
                                        FullName = p.FirstName + " " + p.LastName
                                    }).Distinct().ToList();

                ddEmployee.DataSource = theProducers;
                ddEmployee.DataTextField = "FullName";
                ddEmployee.DataValueField = "PersonId";
                ddEmployee.DataBind();

                //SalesRoles
                var theSalesRoles = (from r in DE.SalesRoles
                                     select new
                                     {
                                         r.SalesRoleId,
                                         r.RoleName
                                     }).Distinct().ToList();

                ddProducerRole.DataSource = theSalesRoles;
                ddProducerRole.DataTextField = "RoleName";
                ddProducerRole.DataValueField = "SalesRoleId";
                ddProducerRole.SelectedIndex = 1;
                ddProducerRole.DataBind();
            }

            txtStartDate.Text = DateTime.Today.ToShortDateString();
        }


        protected void btnSaveContract_Click(object sender, EventArgs e)
        {

        }

        protected void btnReset_Click(object sender, EventArgs e)
        {

        }

        protected void BtnAddBizLine_Click(object sender, EventArgs e)
        {
            using (var DE = new dominiumEntities())
            {
                string Comm = string.Empty;
            _contractLoB.LineOfBusinessId = Convert.ToInt32(ddBizLine.SelectedValue);
             _contractLoB.CarrierId = Convert.ToInt32(ddCarrier.SelectedValue);
            _contractLoB.InsuranceTypeId =  Convert.ToInt32(ddInsuType.SelectedValue);
            _contractLoB.Commission = Convert.ToDecimal(txtLoBComm.Text);

            //var LinesOfBiz = (from i in DE.LineOfBusinesses
            //                  from t in DE.InsuranceTypes
            //                  from c in DE.Carriers
            //                  where i.LineOfBusinessId == _contractLoB.LineOfBusinessId && t.InsuranceTypeId == _contractLoB.InsuranceTypeId && c.CarrierId == _contractLoB.CarrierId
            //                  select new { i.LineOfBusinessId, i.LoBName, t.InsuranceTypeId, t.InsuranceTypeName, c.CarrierId, c.CarrierName, _contractLoB.Commission }
            //                      ).ToList();
  //iLinesOfBiz.Add(LinesOfBiz);


            iContractLinesOfBiz.Add(_contractLoB);
          
            //Biz Lines added
            gvBizLines.DataSource = iContractLinesOfBiz;
            gvBizLines.DataBind();
            }
        }

        protected void BtnAddProducer_Click(object sender, EventArgs e)
        {
            _salesinfo.Commission = Decimal.Parse(txtEmployeeComm.Text);
            _salesinfo.SalesRoleId = Convert.ToInt32(ddProducerRole.SelectedValue);
            _salesinfo.PersonId = Convert.ToInt32(ddEmployee.SelectedValue);
            _salesinfo.Fee = Decimal.Parse(txtProdFee.Text);


            iSalesInfo.Add(_salesinfo);
            gvProducers.DataSource = iSalesInfo;
            gvProducers.DataBind();
        }
    }
}